const pageHelper = require("../../../../../helper/page_helper.js");
const ProjectBiz = require("../../../biz/project_biz.js");
const PassportBiz = require("../../../../../comm/biz/passport_biz.js");

import { apiLogin } from "../../../../../api/index.js";

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
		isEdit: false,

		agree: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);

		await this._loadDetail();
	},

	_loadDetail: async function (e) {
		let opts = {
			title: "bar"
		};
		// let user = await cloudHelper.callCloudData('passport/my_detail', {}, opts);
		// if (user) {
		// 	return wx.reLaunch({ url: '../index/my_index' });
		// }

		this.setData({
			isLoad: true,

			// fields: projectSetting.USER_FIELDS,

			userName: "",
			passWord: "",
			formForms: []
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () { },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () { },

	bindGetPhoneNumber: async function (e) {
		PassportBiz.getPhone(e, this);
	},

	bindAgreeTap: function (e) {
		this.setData({ agree: !this.data.agree });
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	bindSubmitTap: async function (e) {
		if (!this.data.agree) {
			pageHelper.showModal("请先阅读并同意 《用户使用协议》", "温馨提示");
			return;
		}
		try {
			let data = this.data;

			if (!data.userName) {
				pageHelper.showModal("账号不能为空");
				return;
			}
			if (!data.passWord) {
				pageHelper.showModal("密码不能为空");
				return;
			}



			const res = await apiLogin({
				userName: data.userName,
				passWord: data.passWord
			});

			if (res.data === "fail") {
				pageHelper.showModal("账号密码不正确");
				return;
			} else {
				let callback = () => {
					wx.reLaunch({ url: "../index/my_index" });
				};
				wx.setStorageSync('userName', data.userName)
				pageHelper.showSuccToast('登录成功', 1500, callback);
			}


		} catch (err) {
			console.error(err);
		}
	}
});
